<?php
declare(strict_types=1);
namespace SixShop\Message\Entity;

use SixShop\Core\Entity\BaseEntity;
use SixShop\Message\Model\MessageNotificationsModel;
use think\Paginator;
use function SixShop\Core\throw_logic_exception;

/**
 * @mixin MessageNotificationsModel
 */
class MessageNotificationsEntity extends BaseEntity
{
    public function getList(array $params, array $pageAndLimit): Paginator
    {
        return $this->withSearch(['user_id', 'is_read', 'admin_id'], $params)
            ->paginate($pageAndLimit);
    }

    public function read(int $id): self
    {
        $entity = $this->findOrEmpty($id);
        if ($entity->isEmpty()) {
            throw_logic_exception('消息不存在');
        }
        $entity->is_read = 1;
        $entity->read_time = time();
        $entity->save();
        return $entity;
    }
}